/*
 * @Author: Lin Hongwei
 * @Date: 2022-10-29 15:13:12
 * @LastEditTime: 2022-10-30 23:20:22
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 看板controller层
 */
const { queryKanbans, delete:deleteKanban, create, sort, update } = require("../service/kanban.service")

class KanbanController {
  async getKanbanList(ctx, next) {
    try {
      const { projectId } = ctx.params;
      let params = null;
      if (projectId) {
        params = {
          projectId
        }
      }
      const res = await queryKanbans(params)
      ctx.success(res)
    } catch (err) {
      ctx.fail('查询看板列表失败!')
    }
  }
  async createKanban(ctx, next) {
    try {
      const res = await create(ctx.state.kanbanForm);
      if (!!res.affectedRows) return ctx.success();
      ctx.fail("创建看板失败!");
    } catch (err) {
      ctx.fail("创建看板失败!");
    }
  }

  async sortKanban(ctx,next){
    try {
      const res = await sort(ctx.state.sortForm);
      ctx.success();
    } catch (err) {
      ctx.fail("看板排序更新失败!");
    }
  }
  async deleteKanbanById(ctx,next){
    try {
      const res = await deleteKanban(ctx.request.body)
      if (!!res.affectedRows) return ctx.success();
      ctx.fail("删除看板失败!")
    } catch (err) {
      ctx.fail("删除看板失败!");
    }
  }
  async updateKanbanById(ctx, next) {
    let data = ctx.request.body;
    let params = [data, { id: data.id }];
    delete params[0].id;
    try {
      const res = await update(params);
      if (res.affectedRows > 0) return ctx.success();
      ctx.fail("更新看板失败!")
    } catch (err) {
      ctx.fail("更新看板失败!")
    }
  }
}

module.exports = new KanbanController()
